
package CCT220_Trabalho04_13106.control;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import CCT220_Trabalho04_13106.model.Disciplina;
import CCT220_Trabalho04_13106.view.LimiteDisciplina;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Trabalho04_13106.control
 * </br>Class: ControleDisciplina
 * </br>Last Change: 08/10/2011 11:21:35 by marcel
 * </br>Description: Class that controls the operations for the entity of
 * Disciplina
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class ControleDisciplina {
	/**
	 * LimiteDisciplina
	 */
	private final LimiteDisciplina limDisciplina;
	/**
	 * Vector<Disciplina>
	 */
	private Vector<Disciplina>     listaDisciplina;


	/**
	 * </br>******************************
	 * </br>Constructor for
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>LastChange: 08/10/2011 11:21:36 by marcel
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public ControleDisciplina() throws Exception {

		this.limDisciplina = new LimiteDisciplina();
		this.listaDisciplina = new Vector<Disciplina>();
		this.desserializaDisciplina();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: run
	 * </br>Last Change: 08/10/2011 11:21:39 by marcel
	 * </br>Description: Method that call the menu and manage the users options
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	public void run() throws Exception {

		int option = -1;
		while (true) {
			option = this.limDisciplina.montaMenu();
			if (option == 5) {
				this.serializaDisciplina();
				break;
			}
			switch (option) {
				case 1:
					this.cadastra();
					break;
				case 2:
					this.listaUm();
					break;
				case 3:
					this.listaTudo();
					break;
				case 4:
					this.removeUm();
					break;
			}
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: cadastra
	 * </br>Last Change: 08/10/2011 11:21:42 by marcel
	 * </br>Description: Method that call the data insertion and insert the
	 * values in the list
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void cadastra() throws Exception {

		String[] dados = this.limDisciplina.capturaDadosCadastrais();
		this.adicionaNaLista(new Disciplina(Integer.parseInt(dados[0]),
		        dados[1]));
		this.serializaDisciplina();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: listaUm
	 * </br>Last Change: 08/10/2011 11:21:45 by marcel
	 * </br>Description: Method that show just one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaUm() {

		int selectedOne = -1;
		do {
			selectedOne = this.limDisciplina.selectOne(this.getList());
		} while (!this.isInList(selectedOne));
		this.limDisciplina.show(this.getFromList(selectedOne).toString());
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: listaTudo
	 * </br>Last Change: 08/10/2011 11:21:47 by marcel
	 * </br>Description: Method that show all elements from the list
	 * </br>******************************</br>
	 * 
	 */
	private void listaTudo() {

		this.limDisciplina.showAll(this.getList());
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: removeUm
	 * </br>Last Change: 08/10/2011 11:21:49 by marcel
	 * </br>Description: Method that removes one element from the list
	 * </br>******************************</br>
	 * 
	 */
	private void removeUm() {

		Integer selectedOne = -1;
		do {
			selectedOne = this.limDisciplina.selectOne(this.getList());
		} while (!isInList(selectedOne));
		if (selectedOne != null) {
			String description = this.getFromList(selectedOne).toString();
			this.listaDisciplina.remove(this.getFromList(selectedOne));
			this.limDisciplina.show(description + " foi removida!");
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: adicionaNaLista
	 * </br>Last Change: 08/10/2011 11:21:52 by marcel
	 * </br>Description: Method that add an element to the list
	 * </br>******************************</br>
	 * 
	 * @param disciplina
	 */
	public void adicionaNaLista(Disciplina disciplina) {

		this.listaDisciplina.add(disciplina);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: getList
	 * </br>Last Change: 08/10/2011 11:21:54 by marcel
	 * </br>Description: Method that get the whole list in a
	 * LinkedList<String> form
	 * </br>******************************</br>
	 * 
	 * @return LinkedList<String>
	 */
	public LinkedList<String> getList() {

		LinkedList<String> lista = new LinkedList<String>();
		Iterator<Disciplina> it = this.listaDisciplina.iterator();
		while (it.hasNext()) {
			lista.add(it.next().toString());
		}
		return lista;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: isInList
	 * </br>Last Change: 08/10/2011 11:21:56 by marcel
	 * </br>Description: Method that verifies if the given selected element code
	 * is in the list
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return true | false
	 */
	private boolean isInList(int selectedOne) {

		Iterator<Disciplina> it = this.listaDisciplina.iterator();
		while (it.hasNext()) {
			if (it.next().getCodigo() == selectedOne) { return true; }
		}
		return false;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: getFromList
	 * </br>Last Change: 08/10/2011 11:21:58 by marcel
	 * </br>Description: Method that get one element from the list by the given
	 * selected element code
	 * </br>******************************</br>
	 * 
	 * @param selectedOne
	 * @return Disciplina
	 */
	public Disciplina getFromList(int selectedOne) {

		Iterator<Disciplina> it = this.listaDisciplina.iterator();
		while (it.hasNext()) {
			Disciplina d = it.next();
			if (d.getCodigo() == selectedOne) { return d; }
		}
		return null;
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: serializaDisciplina
	 * </br>Last Change: 08/10/2011 11:22:00 by marcel
	 * </br>Description: Method that serializes the list of elements to a file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	private void serializaDisciplina() throws Exception {

		FileOutputStream fos = new FileOutputStream("disciplinas.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(this.listaDisciplina);
		oos.flush();
		oos.close();

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Trabalho04_13106.control
	 * </br>Class: ControleDisciplina
	 * </br>Method: desserializaDisciplina
	 * </br>Last Change: 08/10/2011 11:22:02 by marcel
	 * </br>Description: Method that desserializes the list of elements from a
	 * file
	 * </br>******************************</br>
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void desserializaDisciplina() throws Exception {

		File file = new File("disciplinas.dat");
		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			this.listaDisciplina = (Vector<Disciplina>) ois.readObject();
			ois.close();
		}

	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	@Override
	public void finalize() throws Exception {

		this.serializaDisciplina();
	}

}
